﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MEMDB
{
    public class ResultDataSet
    {

        struct ColumnHeader
        {
            public string Column_name;
            public int Data_Type;
        }

        List<ColumnHeader> Column_List = new List<ColumnHeader>();

        List<List<Object>> Data_Table = new List<List<object>>();

        public bool isEmpty()
        {
            return Column_List.Count == 0;
        }
        public void FillGridView(DataGridView dg)
        {
            dg.Columns.Clear();
            dg.Rows.Clear();
            int k = 0;
            int allWidth = dg.Width;
            foreach (ColumnHeader ch in Column_List)
            {
                dg.Columns.Add("col" + k, ch.Column_name);
                ++ k;
            }
            int oneWidth = allWidth / k;

            if (((List<Object>)Data_Table[0]).Count > 0)
            {
                // Set the columns count
                dg.Rows.Add(((List<Object>)Data_Table[0]).Count);

                for (int i = 0; i < Data_Table.Count; ++i)
                {
                    dg.Columns[i].Width = oneWidth;

                    try
                    {
                        List<Object> col = (List<Object>)Data_Table[i];
                        int j = 0;
                        foreach (Object obj in col)
                        {
                            switch (Column_List[i].Data_Type)
                            {
                                case 0:
                                    Int32 itg = (Int32)obj;
                                    dg.Rows[j].Cells[i].Value = itg;
                                    break;
                                case 1:
                                    Double db = (Double)obj;
                                    dg.Rows[j].Cells[i].Value = db;
                                    break;
                                case 2:
                                    String str = (String)obj;
                                    dg.Rows[j].Cells[i].Value = str;
                                    break;

                            }
                            ++j;
                        }
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
            }
        }

        public bool Convert_Data(byte[] data_buffer)
        {
            int current_pos = 0;
            int str_size = 0;
            byte[] temp_buffer;
            byte[] temp_int = new byte[4];

            int data_int;
            double data_double;
            string data_string;

            int column_size = BitConverter.ToInt32(data_buffer, current_pos);
            current_pos += sizeof(int);

            ColumnHeader Column_Info;

            for (int i = 0; i < column_size; i++)
            {
                Column_Info = new ColumnHeader();

                Column_Info.Data_Type = BitConverter.ToInt32(data_buffer, current_pos);
                current_pos += sizeof(int);

                str_size = BitConverter.ToInt32(data_buffer, current_pos);
                current_pos += sizeof(int);

                temp_buffer = new byte[str_size];
                Buffer.BlockCopy(data_buffer, current_pos, temp_buffer, 0, str_size);
                current_pos += str_size;

                Column_Info.Column_name = Encoding.Unicode.GetString(temp_buffer);

                Column_List.Add(Column_Info);
            }

            int tuple_num = BitConverter.ToInt32(data_buffer, current_pos);
            current_pos += sizeof(int);

            for (int i = 0; i < column_size; i++)
            {
                Data_Table.Add(new List<object>());
            }

            for (int i = 0; i < tuple_num; i++)
            {
                for (int j = 0; j < column_size; j++)
                {
                    switch (Column_List[j].Data_Type)
                    {
                        case 0:
                            data_int = BitConverter.ToInt32(data_buffer, current_pos);
                            current_pos += sizeof(int);
                            Data_Table[j].Add(data_int);

                            break;

                        case 1:
                            data_double = BitConverter.ToDouble(data_buffer, current_pos);
                            current_pos += sizeof(double);
                            Data_Table[j].Add(data_double);

                            break;

                        case 2:
                            str_size = BitConverter.ToInt32(data_buffer, current_pos);
                            current_pos += sizeof(int);

                            temp_buffer = new byte[str_size];
                            Buffer.BlockCopy(data_buffer, current_pos, temp_buffer, 0, str_size);
                            current_pos += str_size;

                            data_string = Encoding.Unicode.GetString(temp_buffer);

                            Data_Table[j].Add(data_string);

                            break;
                    }
                }
            }

            return true;
        }
        public int getRowCount()
        {
            if (Data_Table != null && Data_Table.Count > 0)
            {
                if(Data_Table[0] != null)
                {
                    return Data_Table[0].Count;
                }
            }
            return 0;
        }
    }

}
